package com.wgke.bluetooth.view;

import java.util.Random;

public class HeatMapData {
    // 生成示例热力图数据（高斯分布）
    public static float[][] generateSampleData(int rows, int cols) {
        float[][] data = new float[rows][cols];
        Random random = new Random(42); // 固定随机种子，确保结果可复现

        // 生成3个热点
        for (int i = 0; i < 3; i++) {
            float centerX = random.nextFloat() * cols;
            float centerY = random.nextFloat() * rows;
            float maxValue = 0.8f + random.nextFloat() * 0.2f;
            float radius = cols / 8 + random.nextFloat() * cols / 8;

            for (int y = 0; y < rows; y++) {
                for (int x = 0; x < cols; x++) {
                    // 计算到热点中心的距离
                    float dx = x - centerX;
                    float dy = y - centerY;
                    float distance = (float) Math.sqrt(dx * dx + dy * dy);

                    // 高斯分布：距离越近，值越大
                    if (distance < radius) {
                        float factor = 1 - (distance / radius);
                        data[y][x] = Math.max(data[y][x], maxValue * factor);
                    }
                }
            }
        }

        // 归一化到0-1范围
        float max = 0;
        for (int y = 0; y < rows; y++) {
            for (int x = 0; x < cols; x++) {
                max = Math.max(max, data[y][x]);
            }
        }
        if (max > 0) {
            for (int y = 0; y < rows; y++) {
                for (int x = 0; x < cols; x++) {
                    data[y][x] /= max;
                }
            }
        }

        return data;
    }

    // 从位图获取热力图数据（实际项目中可替换此方法）
    public static float[][] getHeatMapFromBitmap(android.graphics.Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float[][] data = new float[height][width];

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int color = bitmap.getPixel(x, y);
                // 假设位图为灰度图，从Alpha通道获取值（0-255）
                int value = (color >> 24) & 0xff;
                data[y][x] = value / 255.0f;
            }
        }
        return data;
    }
}